<?php 
class stockSpecModel extends Model{
	public $table='stock';
	public function get($gtid,$gid=null){
		// 获得商品的库存
		$stockData=M('stock')->where("gid=$gid")->all();
		
		//获得商品属性类型
		$attrClassData=M('attr_class')->where(array('gtid'=>$gtid,'is_spec'=>1))->all();
		$htmlData=array();
		foreach($stockData as $stock ){
			$html="<tr class='btn'>";
			foreach ($attrClassData as $n => $class) {
				$field=M('attr_value')->where(array('acid'=>$class['acid']))->all();
				$attrClassData[$n]['html']=$this->box($field,$stock['stid']);
				$html.=$attrClassData[$n]['html'];
			}
			
			//库存
			$html.='<td><input type="text" name="spec[stock][]" class="w100" value="'.$stock['stock'].'"></td>';
			//价格
			$html.='<td><input type="text" name="spec[price][]" class="w100" value="'.$stock['price'].'"></td>';
			//货号
			$html.='<td><input type="text" name="spec[goods_sn][]" class="w100" value="'.$stock['goods_sn'].'"></td>';
			$html.='<td><a href="javascript:;" onclick="removeStock(this)" style="color:red;">删除（-）</a></td>';
			$html.='<td><input type="hidden" name="spec[stid][]" value="'.$stock['stid'].'"></td>';
			$html.="</tr>";
			$htmlData[]=$html;
		}
		return array('attrClassData'=>$attrClassData,'htmlData'=>$htmlData);
	}	

	public function box($fields,$stid){
		$attr=M('stock_attr')->where("stid=$stid")->getField('avid,stid');
		$html ='<td><select name="spec[attr]['.$fields[0]['acid'].'][]">';

		foreach($fields as $field){
			$selected = isset($attr[$field['avid']])?'selected=""':'';
			$value = $field['avid'];
			$html.="<option value='{$value}|{$field['attr_value']}' $selected>{$field['attr_value']}</option>";
		}
		$html.="</select></td>";
		return $html;
	}




}


 ?>